#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: ct_data_catasearch.py 
@version:
@time: 2022/05/05 
"""
import time

import allure

from TapClientConnector import TapClientConnector
from TapClientCore.T06_Tapdata_feature.v2.TPF05_meta.tpf_meta import TPFMeta
from TapClientCore.T07_CaseTemplate.v2.CT01_datasource.ct_datasource import CTDataSource
from TapUtil.logfunc.logger import logger


class CTDataSearch(TPFMeta, CTDataSource):
    @staticmethod
    def common_wait():
        with allure.step('加载数据源后的等待'):
            wait_time = 300
            logger.info(f'加载数据源后的等待: {wait_time}')
            time.sleep(wait_time)

    @staticmethod
    def init_connector(db_info):
        return TapClientConnector(db_info)

    @staticmethod
    def drop_target_table(connector: TapClientConnector):
        logger.info('开始进入目标表删除阶段')
        table = connector.tapclient_connector.CURRENT_TABLE
        if connector.database_type in ['elasticsearch']:
            table = table.lower()
        for raw_table in connector.tapclient_connector.show_tables():
            if table == raw_table:
                with allure.step('目标表删除'):
                    logger.info(f'发现目标要删除的表{table}')
                    drop_operate = connector.tapclient_connector.generate_drop(table)
                    connector.tapclient_connector.exec(drop_operate)
                    logger.info(f'删除目标要同步的表{table}')
                    return
        logger.info(f'没有发现目标要删除的表{table}')

    def ct_delete_meta_data_search(self, data):
        table = ''
        db_info = data.get('db_info')
        create_insert_column = data.get('create')
        source_insert_column = data.get('insert')
        try:
            with allure.step('添加表，加载数据源，模型字段获取'):
                # 数据源表获取
                with allure.step('连接数据源'):
                    _source = self.init_connector(db_info)
                with allure.step('增加表'):
                    table = _source.tapclient_connector.CURRENT_TABLE
                    create_str = _source.tapclient_connector.generate_create(create_insert_column)
                    insert_str = _source.tapclient_connector.generate_insert(source_insert_column)
                    _source.tapclient_connector.exec(create_str)
                    _source.tapclient_connector.exec(insert_str)
                # 数据源schema加载
                with allure.step('加载数据源'):
                    logger.info('模型初次加载')
                    source_id = self.DATA_SOURCE_TEMPLATE_CREATE(db_info)
                    self.common_wait()
                # 数据查询
                with allure.step('数据查询-table'):
                    meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(source_id, table).get('id')
                    logger.info(f'new meta id is {meta_id}')
                    self.search_table_only_by_meta_id(table, meta_id)
                with allure.step('meta 获取'):
                    fields_1 = self.get_meta_fields_by_id(meta_id)
            with allure.step('删除数据模型'):
                logger.info(f'删除数据模型id: {meta_id}')
                self.delete_metadataInstances_by_id(meta_id)
            # 数据源schema加载
            with allure.step('再次加载数据源，模型字段获取'):
                logger.info('模型二次加载')
                self.DATA_SOURCE_TEMPLATE_CREATE(db_info)
                self.common_wait()
                # 数据查询
                with allure.step('数据查询-table'):
                    meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(source_id, table).get('id')
                    logger.info(f'new meta id is {meta_id}')
                    self.search_table_only_by_meta_id(table, meta_id)
                with allure.step('meta 获取'):
                    fields_2 = self.get_meta_fields_by_id(meta_id)
            logger.info(f'field 1 {fields_1}')
            logger.info(f'field 2 {fields_2}')
            assert fields_1 == fields_2
        finally:
            with allure.step('连接数据源'):
                _source = self.init_connector(db_info)
                _source.tapclient_connector.CURRENT_TABLE = table
            self.drop_target_table(_source)

    def ct_patch_meta_data_search(self, data):
        table = ''
        db_info = data.get('db_info')
        create_insert_column = data.get('create')
        source_insert_column = data.get('insert')
        try:
            with allure.step('添加表，加载数据源，模型字段获取'):
                # 数据源表获取
                with allure.step('连接数据源'):
                    _source = self.init_connector(db_info)
                with allure.step('增加表'):
                    table = _source.tapclient_connector.CURRENT_TABLE
                    create_str = _source.tapclient_connector.generate_create(create_insert_column)
                    insert_str = _source.tapclient_connector.generate_insert(source_insert_column)
                    _source.tapclient_connector.exec(create_str)
                    _source.tapclient_connector.exec(insert_str)
                # 数据源schema加载
                with allure.step('加载数据源'):
                    source_id = self.DATA_SOURCE_TEMPLATE_CREATE(db_info)
                self.common_wait()
                # 数据查询
                with allure.step('数据查询-table'):
                    meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(source_id, table).get('id')
                    self.search_table_only_by_meta_id(table, meta_id)
                with allure.step('meta 获取'):
                    fields_1 = self.get_meta_fields_by_id(meta_id)
            with allure.step('patch数据模型,模型比对patch和新获取的模型'):
                logger.info(f'patch数据模型id{meta_id}')
                self.patch_meta_fields_by_id(meta_id, payload=fields_1[0])
                tmp_fields = self.get_meta_fields_by_id(meta_id)
                logger.info(f'patch field 1 {fields_1[0]}')
                logger.info(f'patch field 2 {tmp_fields[0]}')
                assert fields_1[0] == tmp_fields[0]
            # 数据源schema加载
            with allure.step('再次加载数据源，模型字段获取'):
                self.DATA_SOURCE_TEMPLATE_CREATE(db_info)
                self.common_wait()
                # 数据查询
                with allure.step('数据查询-table'):
                    meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(source_id, table).get('id')
                    self.search_table_only_by_meta_id(table, meta_id)
                with allure.step('meta 获取'):
                    fields_2 = self.get_meta_fields_by_id(meta_id)
            logger.info(f'field 1 {fields_1}')
            logger.info(f'field 2 {fields_2}')
            assert fields_1 == fields_2
        finally:
            with allure.step('连接数据源'):
                _source = self.init_connector(db_info)
                _source.tapclient_connector.CURRENT_TABLE = table
            self.drop_target_table(_source)

    def ct_data_search_column(self, data):
        table = ''
        db_info = data.get('db_info')
        create_insert_column = data.get('create')
        source_insert_column = data.get('insert')
        try:
            with allure.step('添加表，加载数据源，模型字段获取'):
                # 数据源表获取
                with allure.step('连接数据源'):
                    _source = self.init_connector(db_info)
                with allure.step('增加表'):
                    table = _source.tapclient_connector.CURRENT_TABLE
                    create_str = _source.tapclient_connector.generate_create(create_insert_column)
                    insert_str = _source.tapclient_connector.generate_insert(source_insert_column)
                    _source.tapclient_connector.exec(create_str)
                    _source.tapclient_connector.exec(insert_str)
                # 数据源schema加载
                with allure.step('加载数据源'):
                    source_id = self.DATA_SOURCE_TEMPLATE_CREATE(db_info)
                self.common_wait()
                # 数据查询
                with allure.step('数据查询-table'):
                    meta_id = self.get_metadataInstances_id_by_source_id_and_table_name(source_id, table).get('id')
                with allure.step('meta field_name 获取'):
                    field_name = self.get_field_name_by_id(meta_id)
                    logger.info(f'field_name -> {field_name}')
                    logger.info(f'meta_id -> {meta_id}')
                # 数据查询
                with allure.step('数据查询-column'):
                    meta_id = self.search_column_only_by_meta_id(field_name, meta_id)
                with allure.step('meta 获取 所有fields'):
                    fields = self.get_all_field_name_by_id(meta_id)
            logger.info(f'field_name {field_name}')
            logger.info(f'all fields {fields}')
            assert field_name in fields
        finally:
            with allure.step('连接数据源'):
                _source = self.init_connector(db_info)
                _source.tapclient_connector.CURRENT_TABLE = table
            self.drop_target_table(_source)
